goog.provide('czk.ui.grid.Col');

goog.require('goog.dom');
goog.require('goog.dom.TagName');

goog.require('czk.ui.ZKComponent');

/**
 * @constructor
 * @extends {czk.ui.ZKComponent}
 */
czk.ui.grid.Col = function() {
  goog.base(this);
  this.hasZKChildren = false;
};
goog.inherits(czk.ui.grid.Col, czk.ui.ZKComponent);

czk.ui.grid.Col.prototype.sortDescending = null;
czk.ui.grid.Col.prototype.sortAscending = null;
czk.ui.grid.Col.prototype.sortDirection = null;
czk.ui.grid.Col.prototype.sortA = null;

czk.ui.grid.Col.prototype.createDom = function() {
  var dh = this.getDomHelper();
  var el = dh.createDom(goog.dom.TagName.TH, {});
  this.setElementInternal(el);

  this.sortA = dh.createDom(goog.dom.TagName.A, {
    'className': this.getSortStyle()
  }, this.label);

  goog.dom.insertChildAt(el, dh.createDom(goog.dom.TagName.SPAN, {
    'className': 'theaddiv'
  }, this.sortA));

};

czk.ui.grid.Col.prototype.getSortStyle = function() {
  var sortStyle = 'cell-header';
  if (this.sortDirection == 'ascending')
    sortStyle = 'cell-header asc-sorted';
  else if (this.sortDirection == 'descending')
    sortStyle = 'cell-header desc-sorted';
  return sortStyle;
};

czk.ui.grid.Col.prototype.addChild = function(child, opt_render) {
  // new td for each child
  var dh = this.getDomHelper();
  this.td = dh.createDom(goog.dom.TagName.TR);
  goog.dom.insertChildAt(this.getElement(), this.td);

  goog.base(this, 'addChild', child, opt_render);

};

czk.ui.grid.Col.prototype.initialize =
    function(name, parameter, portlet, children) {
      goog.base(this, 'initialize', name, parameter, portlet, children);
      if (parameter) {
        if (parameter['sortDescending'])
          this.sortDescending = parameter['sortDescending'];
        if (parameter['sortAscending'])
          this.sortAscending = parameter['sortAscending'];
        if (parameter['sortDirection'])
          this.sortDirection = parameter['sortDirection'];
      }
    };

czk.ui.grid.Col.prototype.enterDocument =
    function() {
      goog.base(this, 'enterDocument');
      this.getHandler().listen(this.sortA, goog.events.EventType.CLICK,
          this.action, false, this);

    };

czk.ui.grid.Col.prototype.action = function(e) {
  this.portlet.sendXhr(this, 'onSort');
};

czk.ui.grid.Col.create = function() {
  return new czk.ui.grid.Col();
};
